package ca.kooki.codesnippet;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import ca.kooki.codesnippet.core.TagCloud;
import ca.kooki.codesnippet.core.User;

public class StatisticSummaryTest {

  User aforward, james;
  User unknown;
  
  @Before
  public void setUp()
  {
    aforward = new User("aforward");
    james = new User("james");
    unknown = new User("unknown");
  }
  
  
  @Test
  public void TopPosters()
  {
    StatisticSummary summary = new StatisticSummary();
    
    summary.addTopPoster(aforward,10);
    summary.addTopPoster(james,5);
    
    User[] topPosters = summary.topPosters();
    Assert.assertEquals(2,topPosters.length);
    Assert.assertEquals(aforward,topPosters[0]);
    Assert.assertEquals(james,topPosters[1]);
    
    Assert.assertEquals(10, summary.numberOfPostsFor(aforward));
    Assert.assertEquals(5, summary.numberOfPostsFor(james));
  }
  
  @Test
  public void AddTagsDoNotDuplicateSnippetInCounts()
  {
    CodeSnippet smallSnippet = new CodeSnippet("st","x","12345");
    CodeSnippet mediumSnippet = new CodeSnippet("mt","x","1234567890");
    
    smallSnippet.setId("1");
    mediumSnippet.setId("2");
    
    Tag small = new Tag("small title","ahh bee csis",smallSnippet,"aforward");
    Tag medium = new Tag("medium title","ahh bee",mediumSnippet,"aforward");
    Tag anotherMedium = new Tag("medium title","ahh",mediumSnippet,"james");
    
    StatisticSummary summary = new StatisticSummary();
    
    summary.addTag(anotherMedium);
    summary.addTag(medium);
    summary.addTag(small);
    
    Assert.assertEquals(2, summary.numberOfCodeSnippets());
  }
  
  
  @Test
  public void AddTags()
  {
    
    CodeSnippet smallSnippet = new CodeSnippet("st","x","12345");
    CodeSnippet mediumSnippet = new CodeSnippet("mt","x","1234567890");
    CodeSnippet largeSnippet = new CodeSnippet("lt","x","12345678901234567890");
    
    Tag small = new Tag("small title","ahh bee csis",smallSnippet,"aforward");
    Tag medium = new Tag("medium title","ahh bee",mediumSnippet,"aforward");
    Tag large = new Tag("large title","ahh",largeSnippet,"james");
    
    StatisticSummary summary = new StatisticSummary();
    
    summary.addTag(large);
    summary.addTag(medium);
    summary.addTag(small);
    
    Assert.assertEquals(2, summary.numberOfPostsFor("aforward"));
    Assert.assertEquals(1, summary.numberOfPostsFor("james"));

    User[] allUsers = summary.topPosters();
    Assert.assertEquals("aforward", allUsers[0].getName());
    Assert.assertEquals("james", allUsers[1].getName());
    
    Assert.assertEquals(3, summary.numberOfCodeSnippets());
    Assert.assertEquals(11, summary.averageCodeSnippetLength());
    Assert.assertEquals(20, summary.maximumCodeSnippetLength());
    Assert.assertEquals(5, summary.minimumCodeSnippetLength());
    
    TagCloud cloud = summary.getTagCloud();
    Assert.assertEquals(TagCloud.Magnitude.Smallest, cloud.getMagnitudeOf("csis"));
    Assert.assertEquals(TagCloud.Magnitude.Largest, cloud.getMagnitudeOf("ahh"));
    
  }
  
  
  @Test
  public void UnknownPoster()
  {
    StatisticSummary summary = new StatisticSummary();
    Assert.assertEquals(0, summary.numberOfPostsFor(unknown));
  }
  
  @Test
  public void CodeSnippetLength()
  {
    StatisticSummary summary = new StatisticSummary();
    summary.addCodeSnippetLengths(15,100,350,10);
    
    Assert.assertEquals(15, summary.numberOfCodeSnippets());
    Assert.assertEquals(100, summary.averageCodeSnippetLength());
    Assert.assertEquals(350, summary.maximumCodeSnippetLength());
    Assert.assertEquals(10, summary.minimumCodeSnippetLength());
    
  }
  
  
  
  
  @Test
  public void toHtml()
  {
    String expected = "<html><head>" + 
    "<style>" + 
    ".smallestTag { font-size: xx-small; }" +
    ".smallTag { font-size: small; }" +
    ".mediumTag { font-size: medium; }" +
    ".largeTag { font-size: large; }" +
    ".largestTag { font-size: xx-large; }" + 
    "</style>" +
    "</head>" +
    "<body>" +
    "<h2>Top Posters</h2>" +
    "<table>" +
    "<tr><td>aforward</td><td>10</td></tr>" +
    "<tr><td>james</td><td>5</td></tr>" +
    "</table>" +

    "<h2>Code Snippets</h2>" +
    "<table>" +
    "<tr><td>Average length</td><td>100</td></tr>" +
    "<tr><td>Longest Snippet</td><td>350</td></tr>" +
    "<tr><td>Shortest Snippet</td><td>10</td></tr>" +
    "<tr><td>Number of Entries</td><td>250</td></tr>" +
    "</table>" +
    "<h2>Keyword Density</h2>" + 
    "<div>" + 
    "<span class='largestTag'>ahhh</span> " + 
    "<span class='largeTag'>bee</span> " + 
    "<span class='largeTag'>csis</span> " +
    "<span class='mediumTag'>dumb</span> " + 
    "<span class='mediumTag'>eel</span> " + 
    "<span class='mediumTag'>fudge</span> " + 
    "<span class='smallTag'>good</span> " + 
    "<span class='smallTag'>henry</span> " + 
    "<span class='smallTag'>igloo</span> " + 
    "<span class='smallestTag'>jerry</span> " + 
    "</div>" +
    "</body>" +
    "</html>";

    StatisticSummary summary = new StatisticSummary();
    
    summary.addTopPoster(aforward,10);
    summary.addTopPoster(james,5);
    summary.addCodeSnippetLengths(250, 100, 350, 10);
    summary.addKeywords(new String[] {"ahhh"});
    summary.addKeywords(new String[] {"ahhh","bee"});
    summary.addKeywords(new String[] {"ahhh","bee","csis"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel","fudge"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel","fudge","good"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel","fudge","good","henry"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel","fudge","good","henry","igloo"});
    summary.addKeywords(new String[] {"ahhh","bee","csis","dumb","eel","fudge","good","henry","igloo","jerry"});
    
    
    
    String actual = summary.toHtml();
    
    Assert.assertEquals(expected, actual);
    

  }
  
}
